package com.glodon.paas.document.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.glodon.paas.account.api.bean.User;
import com.glodon.paas.consts.StringConst;
import com.glodon.paas.document.api.bean.File;
import com.glodon.paas.document.service.RecyclerService;
import com.glodon.paas.document.service.util.OrderUtilsFile;
import com.glodon.paas.document.web.util.SecurityUtils;

@Controller
@RequestMapping("/recycler")
public class RecyclerController {

    public static final Logger     LOGGER = LoggerFactory.getLogger(RecyclerController.class);

    @Autowired
    private RecyclerService    recyclerService;
    
    @Autowired
    private ActivityPublisher  activityPublisher;
    
    @RequestMapping(value = "", method = RequestMethod.GET)
    public void getRecycle(@RequestParam(value = "orderBy", required = false, defaultValue = "updateTime") String orderBy,
                           @RequestParam(value = "orderType", required = false) String orderType, 
                           ModelMap model) {
        List<File> files = recyclerService.getFiles(SecurityUtils.getCurrentUser().getId());
        OrderUtilsFile.order(files, orderBy, orderType);
        model.addAttribute(StringConst.ITEMS, files);
    }

    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public void clean(ModelMap model) {
        recyclerService.clean(SecurityUtils.getCurrentUser().getId());
        model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
    }
    
    @RequestMapping(value = "/{fileId}", method = RequestMethod.DELETE)
    public void delete(@PathVariable("fileId") String fileId, ModelMap model) {
        User user = SecurityUtils.getCurrentUser();
        if (StringUtils.isNotEmpty(fileId)) {
            String[] fileIds = fileId.split(StringConst.SEPARATOR_COMMA);
            List<String> failedItems = new ArrayList<String>();
            for (String id : fileIds) {
                try {
                    recyclerService.remove(user.getId(), id);
                } catch (Exception e) {
                    failedItems.add(fileId);
                }
            }
            if (!failedItems.isEmpty()) {
                model.addAttribute(StringConst.FAILED_ITEMS, failedItems);
            }
            model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
        }
    }
  
    @RequestMapping(value = "/{fileId}", method = RequestMethod.PUT)
    public void restore(@PathVariable("fileId") String fileId, ModelMap model) {
        User user = SecurityUtils.getCurrentUser();
        List<String> failedItems = new ArrayList<String>();
        List<String> errorItems = new ArrayList<String>();
        List<String> successItems = new ArrayList<String>();
        List<String> resultItems;
        if (StringUtils.isNotEmpty(fileId)) {
            String[] fileIds = fileId.split(StringConst.SEPARATOR_COMMA);
            for (String id : fileIds) {
                resultItems = recyclerService.restore(user.getId(), id);
                if (!resultItems.isEmpty()) {
                    failedItems.addAll(resultItems);
                    errorItems.add(id);
                }else{
                    successItems.add(id);
                }
            }
            if (!failedItems.isEmpty()) {
                model.addAttribute(StringConst.FAILED_ITEMS, failedItems);
                model.addAttribute(StringConst.ERROR_ITEMS, errorItems);
            }
            model.addAttribute(StringConst.ITEMS, successItems);
            model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
        }
        
     // Activity
     activityPublisher.restoreFile(user.getId(), user.getDisplayName(), successItems, true);
    }
}
